﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using System.Data.Entity;
using BugTracker.DataAccess.Data;
using BugTracker.BusinessEntities.Entities.WorkLog;


namespace BugTracker.BusinessLogic.WorkLog
{
    public class WorkLogDetailService : IWorkLogDetailService
    {
        private BugTrackerEntities db = null;

        public WorkLogDetailService(BugTrackerEntities db)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            this.db = db;
        }

        public IList<BusinessEntities.Entities.WorkLog.WorkLogDetail> PageSortWhere(
                                                                                    int pageIndex, 
                                                                                    int pageSize,
                                                                                    string whereClause,
                                                                                    string whereParam,
                                                                                    string sortExpression,
                                                                                    out int count)
        {

            count = (from models in db.WorkLogs
                     join issues in db.Issues on models.IssueID equals issues.IssueID
                     where models.IsDeleted == false & models.UserId == new Guid(whereParam)
                     select models).Count();

            var results = (
                            from models in db.WorkLogs
                            join issues in db.Issues on models.IssueID equals issues.IssueID
                            where models.IsDeleted == false
                            select new WorkLogDetail 
                                {
                                    WorkLogID = models.WorkLogID,
                                    IssueID = models.IssueID,
                                    EntryDate = models.EntryDate,
                                    Detail = models.Detail,
                                    WorkHours = models.WorkHours,
                                    IsActive = models.IsActive,
                                    IsDeleted = models.IsDeleted,
                                    RowGuid = models.RowGuid,
                                    CreatedOn = models.CreatedOn,
                                    ModifiedOn = models.ModifiedOn,
                                    UserId = models.UserId,
                                    IssueName = issues.Name
                                }
                            )
                            .Where(whereClause, whereParam)
                            .OrderBy(sortExpression)
                            .Skip(pageIndex * pageSize)
                            .Take(pageSize);

            return results.ToList<WorkLogDetail>();
        }
    }
}
